<?php

session_start();
if (isset($_SESSION['lang']))
    require_once "./language_files/conferencePageServer_" . $_SESSION['lang'] . ".php";
else
    require_once "./language_files/conferencePageServer_en.php";
include "statics.php";
if (isset($_POST['role'])) {
    $rsConf = rsSelect("conference_id", "conference", array("conference_id=", $_POST['conference_id']));
    if (is_string($rsConf)) {
        echo $rsConf . "ERROR CODE: 0" . __LINE__;
        exit();
    }
    if (mysql_num_rows($rsConf) == 0) {
        echo "ERROR CODE: 0" . __LINE__;
        exit();
    }
    if (bPending($_SESSION['member_id'], $_POST['conference_id'], $_POST['role'])) {
        echo "ERROR CODE: 0" . __LINE__;
        exit();
    }
    $iRoleId = iRoleId($_POST['role']);
    if (is_string($iRoleId)) {
        echo "ERROR CODE: 0" . __LINE__;
        exit();
    }
    if ($iRoleId == 1) {
        $type = "R";
    }
    if ($iRoleId == 2) {
        $type = "O";
    }
    if ($iRoleId == 3) {
        $type = "A";
    }

    $member_id = $_SESSION['member_id'];
    $conference_id = $_POST['conference_id'];
    $has_invitation_request = mysql_query("Select * From member_request_member where member_id2='$member_id' AND
			                  conference_id ='$conference_id' AND type= '$type'");
    if (is_string($has_invitation_request)) {
        echo $rsConf . "ERROR CODE: 0" . __LINE__;
        exit();
    }
    if (mysql_num_rows($has_invitation_request)) {
        mysql_query("INSERT INTO member_privileges(member_id, privileges_id, conference_id)
                  VALUES ('$member_id','$iRoleId','$conference_id')")
                or die(mysql_error());
        mysql_query("UPDATE member_request_member SET approve = 1
                    where member_id2 = '$member_id'
                 AND conference_id = '$conference_id' AND type = '$type'") or die(mysql_error());

        echo $lang['message_1'];

        exit();
    }
    $iRet = iInsert("member_subscribesto_conference", array("member_id" => $_SESSION['member_id'], "conference_id" => $_POST['conference_id'], "request_type" => $iRoleId));
    if ($iRet == 1) {
        echo "1";
    } else {
        echo $iRet . "ERROR CODE: 0" . __LINE__;
    }
} else if (isset($_POST['get_place'])) {
    $rsCountry = rsSelect("*", "country");
    if (is_string($rsCountry))
        echo $rsCountry;
    else {
        while ($row = mysql_fetch_assoc($rsCountry)) {
            echo "<option value='{$row['country_id']}'>" . $row['country_name'] . "</option>";
        }
    }
} else if (isset($_POST['remove'])) {
    $rsRole = rsSelect("COUNT(privileges_id)", "privileges", array("privileges_id=", $_POST['pending_role']));
    if (is_string($rsRole)) {
        echo $rsRole . "ERROR CODE: 0" . __LINE__;
        exit();
    }
    $row = mysql_fetch_assoc($rsRole);
    if ((int) $row['COUNT(privileges_id)'] == 0) {
        echo "ERROR CODE: 0" . __LINE__;
        exit();
    }
    $iRet = iDelete("member_subscribesto_conference", array("member_id=", $_SESSION['member_id'], "AND", "conference_id=", $_POST['conference_id'], "AND", "request_type=", $_POST['pending_role'], "AND", "approve ", "E__IS NULL"));
    if (is_string($iRet)) {
        echo $iRet . "ERROR CODE: 0" . __LINE__;
        exit();
    }
    echo "1";
} else if (isset($_POST['update'])) {
    if (!bOrganizer($_SESSION['member_id'], $_POST['conference_id'])) {
        echo "ERROR CODE: 0" . __LINE__;
        exit();
    }
    if (strlen($_POST['desc']) > 2000 || strlen($_POST['call']) > 200) {
        echo "ERROR CODE: 0" . __LINE__;
        exit();
    }
    $rsConference = rsSelect("*", "conference", array("conference_id=", $_POST['conference_id']));
    if (is_string($rsConference)) {
        echo $rsConference . "ERROR CODE: 0" . __LINE__;
        exit();
    }
    if (mysql_num_rows($rsConference) == 0) {
        echo "ERROR CODE: 0" . __LINE__;
        exit();
    }
    $row = mysql_fetch_assoc($rsConference);
    $dtCurdate = date("Y-m-d");
    $dtConference_date = date("Y-m-d", strtotime($row['conference_date']));
    $regexDate = "/^20(1[0-9]|2[0-9]|3[0-8])(-|\/)\d{2}(-|\/)\d{2}$/";
    if (preg_match($regexDate, $_POST['rev_start']) == 0 || preg_match($regexDate, $_POST['rev_end']) == 0) {
        echo "ERROR CODE: 0" . __LINE__;
        exit();
    }
    $dtReviewStartDate = date("Y-m-d", strtotime($_POST['rev_start']));
    $dtReviewEndDate = date("Y-m-d", strtotime($_POST['rev_end']));
    $dtShortPaperDate = NULL;
    $dtLongPaperDate = NULL;
    $dtPosterDate = NULL;
    $dtImageDate = NULL;
    $dtVideoDate = NULL;
    $dtDBReviewStart = NULL;
    $dtDBReviewEnd = NULL;
    $rsRevDates = rsSelect("rev_startDate,rev_endDate", "conference", array("conference_id=", $_POST['conference_id']));
    if (is_string($rsRevDates)) {
        echo $rsRevDates . "ERROR CODE: 0" . __LINE__;
        exit();
    } else {
        if (mysql_num_rows($rsRevDates) == 0) {
            echo "ERROR CODE: 0" . __LINE__;
            exit();
        }
        $row = mysql_fetch_assoc($rsRevDates);
        $dtDBReviewStart = date("Y-m-d", strtotime($row['rev_startDate']));
        $dtDBReviewEnd = date("Y-m-d", strtotime($row['rev_endDate']));
    }
    if (($dtReviewStartDate <= $dtCurdate || $dtReviewEndDate <= $dtReviewStartDate) && ($dtReviewStartDate != $dtDBReviewStart || $dtReviewEndDate != $dtDBReviewEnd)) {
        echo "ERROR CODE: 0" . __LINE__;
        exit();
    } else if ($dtReviewEndDate <= $dtCurdate && $dtReviewEndDate != $dtDBReviewEnd) {
        echo "ERROR CODE: 0" . __LINE__;
        exit();
    }

    if (isset($_POST['short_paper_sub'])) {
        if (preg_match($regexDate, $_POST['short_paper_sub']) == 0) {
            echo "ERROR CODE: 0" . __LINE__;
            exit();
        } else {
            $dtShortPaperDate = date("Y-m-d", strtotime($_POST['short_paper_sub']));
            $rsShortDate = rsSelect("submission_date", "conference_category", array("conference_id=", $_POST['conference_id'], "AND", "category_id=", iCategoryId("Short Paper")));
            if (is_string($rsShortDate)) {
                echo $rsShortDate . "ERROR CODE: 0" . __LINE__;
                exit();
            } else {
                if (mysql_num_rows($rsShortDate) == 0) {
                    echo "ERROR CODE: 0" . __LINE__;
                    exit();
                }
                $row = mysql_fetch_assoc($rsShortDate);
                $dtDBShortPaperDate = date("Y-m-d", strtotime($row['submission_date']));
                if ($dtShortPaperDate >= $dtReviewStartDate && ($dtReviewStartDate != $dtDBReviewStart || $dtShortPaperDate != $dtDBShortPaperDate)) {
                    echo "ERROR CODE: 0" . __LINE__;
                    exit();
                } else if ($dtShortPaperDate <= $dtCurdate && $dtShortPaperDate != $dtDBShortPaperDate) {
                    echo "ERROR CODE: 0" . __LINE__;
                    exit();
                }
            }
        }
    }
    if (isset($_POST['long_paper_sub'])) {
        if (preg_match($regexDate, $_POST['long_paper_sub']) == 0) {
            echo "ERROR CODE: 0" . __LINE__;
            exit();
        } else {
            $dtLongPaperDate = date("Y-m-d", strtotime($_POST['long_paper_sub']));
            $rsLongDate = rsSelect("submission_date", "conference_category", array("conference_id=", $_POST['conference_id'], "AND", "category_id=", iCategoryId("Long Paper")));
            if (is_string($rsLongDate)) {
                echo $rsLongDate . "ERROR CODE: 0" . __LINE__;
                exit();
            } else {
                if (mysql_num_rows($rsLongDate) == 0) {
                    echo "ERROR CODE: 0" . __LINE__;
                    exit();
                }
                $row = mysql_fetch_assoc($rsLongDate);
                $dtDBLongPaperDate = date("Y-m-d", strtotime($row['submission_date']));
                if ($dtLongPaperDate >= $dtReviewStartDate && ($dtReviewStartDate != $dtDBReviewStart || $dtLongPaperDate != $dtDBLongPaperDate)) {
                    echo "ERROR CODE: 0" . __LINE__;
                    exit();
                } else if ($dtLongPaperDate <= $dtCurdate && $dtLongPaperDate != $dtDBLongPaperDate) {
                    echo "ERROR CODE: 0" . __LINE__;
                    exit();
                }
            }
        }
    }
    if (isset($_POST['poster_sub'])) {
        if (preg_match($regexDate, $_POST['poster_sub']) == 0) {
            echo "ERROR CODE: 0" . __LINE__;
            exit();
        } else {
            $dtPosterDate = date("Y-m-d", strtotime($_POST['poster_sub']));
            $rsPosterDate = rsSelect("submission_date", "conference_category", array("conference_id=", $_POST['conference_id'], "AND", "category_id=", iCategoryId("Poster")));
            if (is_string($rsPosterDate)) {
                echo $rsPosterDate . "ERROR CODE: 0" . __LINE__;
                exit();
            } else {
                if (mysql_num_rows($rsPosterDate) == 0) {
                    echo "ERROR CODE: 0" . __LINE__;
                    exit();
                }
                $row = mysql_fetch_assoc($rsPosterDate);
                $dtDBPosterDate = date("Y-m-d", strtotime($row['submission_date']));
                if ($dtPosterDate >= $dtReviewStartDate && ($dtReviewStartDate != $dtDBReviewStart || $dtPosterDate != $dtDBPosterDate)) {
                    echo "ERROR CODE: 0" . __LINE__;
                    exit();
                } else if ($dtPosterDate <= $dtCurdate && $dtPosterDate != $dtDBPosterDate) {
                    echo "ERROR CODE: 0" . __LINE__;
                    exit();
                }
            }
        }
    }
    if (isset($_POST['image_sub'])) {
        if (preg_match($regexDate, $_POST['image_sub']) == 0) {
            echo "ERROR CODE: 0" . __LINE__;
            exit();
        } else {
            $dtImageDate = date("Y-m-d", strtotime($_POST['image_sub']));
            $rsImageDate = rsSelect("submission_date", "conference_category", array("conference_id=", $_POST['conference_id'], "AND", "category_id=", iCategoryId("Image")));
            if (is_string($rsImageDate)) {
                echo $rsImageDate . "ERROR CODE: 0" . __LINE__;
                exit();
            } else {
                if (mysql_num_rows($rsImageDate) == 0) {
                    echo "ERROR CODE: 0" . __LINE__;
                    exit();
                }
                $row = mysql_fetch_assoc($rsImageDate);
                $dtDBImageDate = date("Y-m-d", strtotime($row['submission_date']));
                if ($dtImageDate >= $dtReviewStartDate && ($dtReviewStartDate != $dtDBReviewStart || $dtImageDate != $dtDBImageDate)) {
                    echo "ERROR CODE: 0" . __LINE__;
                    exit();
                } else if ($dtImageDate <= $dtCurdate && $dtImageDate != $dtDBImageDate) {
                    echo "ERROR CODE: 0" . __LINE__;
                    exit();
                }
            }
        }
    }
    if (isset($_POST['video_sub'])) {
        if (preg_match($regexDate, $_POST['video_sub']) == 0) {
            echo "ERROR CODE: 0" . __LINE__;
            exit();
        } else {
            $dtVideoDate = date("Y-m-d", strtotime($_POST['video_sub']));
            $rsVideoDate = rsSelect("submission_date", "conference_category", array("conference_id=", $_POST['conference_id'], "AND", "category_id=", iCategoryId("Video")));
            if (is_string($rsVideoDate)) {
                echo $rsVideoDate . "ERROR CODE: 0" . __LINE__;
                exit();
            } else {
                if (mysql_num_rows($rsVideoDate) == 0) {
                    echo "ERROR CODE: 0" . __LINE__;
                    exit();
                }
                $row = mysql_fetch_assoc($rsVideoDate);
                $dtDBVideoDate = date("Y-m-d", strtotime($row['submission_date']));
                if ($dtVideoDate >= $dtReviewStartDate && ($dtReviewStartDate != $dtDBReviewStart || $dtVideoDate != $dtDBVideoDate)) {
                    echo "ERROR CODE: 0" . __LINE__;
                    exit();
                } else if ($dtVideoDate <= $dtCurdate && $dtVideoDate != $dtDBVideoDate) {
                    echo "ERROR CODE: 0" . __LINE__;
                    exit();
                }
            }
        }
    }

    $iRet = iUpdate("conference", array("place" => $_POST['country'], "description" => $_POST['desc'], "call_for_papers_text" => $_POST['call'], "rev_startDate" => $_POST['rev_start'], "rev_endDate" => $_POST['rev_end']), array("conference_id=", $_POST['conference_id']));
    if ($iRet != 1) {
        echo $iRet . "ERROR CODE: 0" . __LINE__;
        exit();
    } else {
        if ($dtShortPaperDate != NULL) {
            $iCategoryId = iCategoryId("Short Paper");
            if ($iCategoryId == false || is_string($iCategoryId)) {
                echo "ERROR CODE: 0" . __LINE__;
                exit();
            } else {
                $iRet = iUpdate("conference_category", array("submission_date" => $dtShortPaperDate), array("conference_id=", $_POST['conference_id'], "AND", "category_id=", $iCategoryId));
                if ($iRet != 1) {
                    echo $iRet . "ERROR CODE: 0" . __LINE__;
                    exit();
                }
            }
        }

        if ($dtLongPaperDate != NULL) {
            $iCategoryId = iCategoryId("Long Paper");
            if ($iCategoryId == false || is_string($iCategoryId)) {
                echo "ERROR CODE: 0" . __LINE__;
                exit();
            } else {
                $iRet = iUpdate("conference_category", array("submission_date" => $dtLongPaperDate), array("conference_id=", $_POST['conference_id'], "AND", "category_id=", $iCategoryId));
                if ($iRet != 1) {
                    echo $iRet . "ERROR CODE: 0" . __LINE__;
                    exit();
                }
            }
        }
        if ($dtPosterDate != NULL) {
            $iCategoryId = iCategoryId("Poster");
            if ($iCategoryId == false || is_string($iCategoryId)) {
                echo "ERROR CODE: 0" . __LINE__;
                exit();
            } else {
                $iRet = iUpdate("conference_category", array("submission_date" => $dtPosterDate), array("conference_id=", $_POST['conference_id'], "AND", "category_id=", $iCategoryId));
                if ($iRet != 1) {
                    echo $iRet . "ERROR CODE: 0" . __LINE__;
                    exit();
                }
            }
        }
        if ($dtImageDate != NULL) {
            $iCategoryId = iCategoryId("Image");
            if ($iCategoryId == false || is_string($iCategoryId)) {
                echo "ERROR CODE: 0" . __LINE__;
                exit();
            } else {
                $iRet = iUpdate("conference_category", array("submission_date" => $dtImageDate), array("conference_id=", $_POST['conference_id'], "AND", "category_id=", $iCategoryId));
                if ($iRet != 1) {
                    echo $iRet . "ERROR CODE: 0" . __LINE__;
                    exit();
                }
            }
        }
        if ($dtVideoDate != NULL) {
            $iCategoryId = iCategoryId("Long Paper");
            if ($iCategoryId == false || is_string($iCategoryId)) {
                echo "ERROR CODE: 0" . __LINE__;
                exit();
            } else {
                $iRet = iUpdate("conference_category", array("submission_date" => $dtVideoDate), array("conference_id=", $_POST['conference_id'], "AND", "category_id=", $iCategoryId));
                if ($iRet != 1) {
                    echo $iRet . "ERROR CODE: 0" . __LINE__;
                    exit();
                }
            }
        }
    }
    echo "1";
}
?>
